package com.fis.vaadin.component;

import java.util.Vector;

import com.fis.vaadin.util.FisDefaultTheme;
import com.fis.vaadin.util.MessageAlerter;
import com.fis.vaadin.util.ModuleManager;
import com.fis.vaadin.validator.PanelTreeProvider;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Layout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.CloseHandler;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.Tree;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

public class TreeTabPanel extends CustomComponent implements PanelTreeProvider {
	private HorizontalSplitPanel mainLayout;
	private CommonTreePanel commonTree;
	private TabSheet tabsheet;

	// public static McaAction mcaActionRoot;
	private PanelTreeProvider selectedTab;
	private Object currentTreeNode;
	// private Object tempTreeNode;

	// private final String OBJECT_ACTION_ROOT =
	// TM.get("itrdt.str.object_action_root");
	private ComboBox cboSearch;
	private Tree tree;

	private Layout homeTab;

	public TreeTabPanel() {
		// this.setCaption(TM.get("FormInteractionDetail"));
		this.setSizeFull();
		// this.setStyleName("treeTabPanel");
		initLayout();
	}

	private void initHomeTab() {

		homeTab = new VerticalLayout();
	}

	@SuppressWarnings("serial")
	private void initLayout() {
		initHomeTab();

		mainLayout = new HorizontalSplitPanel();
		this.setCompositionRoot(mainLayout);
		initComponentLeft();
		commonTree = new CommonTreePanel(tree, cboSearch, this);
		tree.addListener(new Property.ValueChangeListener() {
			@Override
			public void valueChange(ValueChangeEvent event) {
				treeValueChanged(tree.getValue());
			}
		});
		tree.addListener(new ItemClickEvent.ItemClickListener() {
			@Override
			public void itemClick(ItemClickEvent event) {
				Object item = event.getItemId();
				if (event.isDoubleClick()) {
					if (tree.isExpanded(item)) {
						tree.collapseItem(event.getItemId());
					} else {
						tree.expandItem(event.getItemId());
					}
				}
			}
		});
		commonTree.setButtonSearchTooltip(TM.get("common.btn.search"));
		String searchTooltip = TM.get("itrdt.tooltip.search");
		commonTree.setComboBoxSearchTooltip(searchTooltip);
		commonTree.setComBoxSearchInputPrompt(searchTooltip);
		initComponent();
	}

	private void initComponent() {
		mainLayout.setSplitPosition(250, Sizeable.UNITS_PIXELS);
		mainLayout.setFirstComponent(commonTree);
		mainLayout.setSecondComponent(initComponentRight());
	}

	private void initComponentLeft() {
		cboSearch = new ComboBox();
		cboSearch.setFilteringMode(ComboBox.FILTERINGMODE_CONTAINS);

		tree = new DecoratedTree();

	}

	public Tree buildTree(Vector vt) {
		return ModuleManager.buildTree(tree, null, vt);
	}

	private TabSheet initComponentRight() {
		tabsheet = new TabSheet();
		tabsheet.setSizeFull();
		tabsheet.setCloseHandler(new CloseHandler() {

			@Override
			public void onTabClose(TabSheet tabsheet, Component tabContent) {
				tabsheet.removeComponent(tabContent);
			}
		});

		Tab tabHome = tabsheet.addTab(homeTab);
		tabHome.setCaption("Home");
		tabHome.setClosable(false);
		tabHome.setIcon(FisDefaultTheme.ICON_ROOT);
		return tabsheet;
	}

	private void selectAndExpand(Object obj) {

	}

	@Override
	public void filterTree(Object obj) {
		selectAndExpand(obj);
	}

	@Override
	public void treeValueChanged(Object value) {
		if (value instanceof TreeItemId) {
			try {
				TreeItemId id = (TreeItemId) value;
				if (id.getModuleType().equals("G")) {
					return;
				}
				this.addTab(id.getRelatedForm());
			} catch (InstantiationException ex) {
				ex.printStackTrace();
				MessageAlerter.showErrorMessage(null, ex.getMessage());
			} catch (IllegalAccessException ex) {
				ex.printStackTrace();
				MessageAlerter.showErrorMessage(null, ex.getMessage());
			} catch (ClassNotFoundException ex) {
				ex.printStackTrace();
				MessageAlerter.showErrorMessage(null, ex.getMessage());
			} catch (NoClassDefFoundError e) {
				// e.printStackTrace();
				MessageAlerter.showErrorMessage(null, e.getMessage());
			}
		}
	}

	public Tab addTab(Component com) {
		Tab tab = tabsheet.getTab(com);

		if (tab == null) {
			tab = tabsheet.addTab(com);
			tab.setClosable(true);
			tab.setCaption(TM.get(com.getClass().getName()));
			// set Icon for Tab
			// tab.setIcon(com.getIcon());
			tab.setIcon(FisDefaultTheme.ICON_ACTION);

		}

		

		tabsheet.setSelectedTab(com);
		// tabsheet.requestRepaintAll();
		tabsheet.setTabPosition(tab, 1);
		return tab;
	}

	public void addTreeValueChangeListener(Property.ValueChangeListener change) {
		tree.addListener(change);
	}

}
